package studentdemo.dao;

import studentdemo.modul.Teach;
import studentdemo.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TeachDaoImpl implements TeachDao {
    @Override
    public int insertTeach(Teach teach) {
        String sql = "INSERT INTO Dujx_Teach11 (djx_Tecno11, djx_Tetno11) VALUES (?, ?)";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, teach.getTecno());
            ps.setString(2, teach.getTetno());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteTeach(String tecno, String tetno) {
        String sql = "DELETE FROM Dujx_Teach11 WHERE djx_Tecno11=? AND djx_Tetno11=?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, tecno);
            ps.setString(2, tetno);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Teach> getTeachesByCourse(String tecno) {
        String sql = "SELECT * FROM Dujx_Teach11 WHERE djx_Tecno11=?";
        List<Teach> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, tecno);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Teach(rs.getString("djx_Tecno11"), rs.getString("djx_Tetno11")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Teach> getTeachesByTeacher(String tetno) {
        String sql = "SELECT * FROM Dujx_Teach11 WHERE djx_Tetno11=?";
        List<Teach> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, tetno);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Teach(rs.getString("djx_Tecno11"), rs.getString("djx_Tetno11")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Teach> getAllTeaches() {
        String sql = "SELECT * FROM Dujx_Teach11";
        List<Teach> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection(); Statement st = conn.createStatement()) {
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                list.add(new Teach(rs.getString("djx_Tecno11"), rs.getString("djx_Tetno11")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
} 